package com.github.thinwonton.cross.access.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 此servlet用来生成token
 */
@WebServlet("/token")
public class TokenGeneratorServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 生成token
        String token = "abcd";

        String cookieName = "token";
        String cookieVal = token;

        //写入本域cookie
        Cookie cookie = new Cookie(cookieName, cookieVal);
        cookie.setPath("/");
        response.addCookie(cookie);

        // 请求的原始来源域
        String from = request.getParameter("from");

        response.sendRedirect(from + "?cname=" + cookieName + "&cval=" + cookieVal); //重定向到目标域
    }

}
